package com.taylor.community.controller;

import com.taylor.community.entity.Comment;
import com.taylor.community.entity.DiscussPost;
import com.taylor.community.entity.Event;
import com.taylor.community.event.EventProducer;
import com.taylor.community.service.CommentService;
import com.taylor.community.service.DiscussPostService;
import com.taylor.community.util.CommunityConstant;
import com.taylor.community.util.HostHolder;
import com.taylor.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
/**
 * 对帖子或帖子的评论 进行评论
 */
@RequestMapping("/comment")
public class CommentController implements CommunityConstant{
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private CommentService commentService;
    @Autowired
    private EventProducer eventProducer;
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping(path = "/add/{discussPostId}",method = RequestMethod.POST)
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        // 补充部分后调用添加评论的方法
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);
        //触发评论事件，系统发送通知
        Event event = new Event()
                .setTopic(TOPIC_COMMENT)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(comment.getEntityType())
                .setEntityId(comment.getEntityId())
                //不管是对帖子还是对帖子的评论的评论 在消息页面都要能够链到帖子详情页
                .setData("postId",discussPostId);
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            DiscussPost discussPost = discussPostService.findDiscussPostById(discussPostId);
            event.setEntityUserId(discussPost.getUserId());
        }
        if (comment.getEntityType() == ENTITY_TYPE_COMMENT){
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        eventProducer.fireEvent(event);
        //评论给帖子时 要添加评论数量 即要修改es服务器上对应的参数 将这些逻辑封装成事件
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            eventProducer.fireEvent(event);
            //对帖子评论操作影响分数 将帖子的id存到redis里 定时计算分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey,discussPostId);


        }
        //重定向到当前的帖子详情的页面
        return "redirect:/discuss/detail/"+ discussPostId;
    }
}
